<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use think\Db;
use think\Validate;

/**
 * 员工管理
 *
 * @icon fa fa-circle-o
 */
class Staff extends Backend
{

    /**
     * Staff模型对象
     * @var \app\admin\model\general\Staff
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\general\Staff;
        $stafflist = DB::name("staff_group")->select();
        $this->view->assign('group',$stafflist);
    }

    public function import()
    {
        parent::import();
    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            //查询当前组一共交易多少金额
            $group_id = 0;
            $filter = $this->request->request()['filter'];
            $filter_arr = json_decode($filter);
            if(isset($filter_arr->group_id) && $filter_arr->group_id){
                $group_id = $filter_arr->group_id;
            }
            if($group_id == 0){
                //查询全部的交易额
                $group_arr = $this->model->where(['group_id'=>['<>',0]])->field('invite')->select();
            }else{
                //查看当前分组以及子分组的交易额
                $group_arrs = DB::name("staff_group")->where('pid',$group_id)->field("id")->select();
                $group_arr1 = array_column($group_arrs,'id');
                $group_arr = $this->model->where(['group_id'=>['in',$group_arr1]])->field('invite')->select();
            }
            $invites = array_column($group_arr,'invite');
            $infos = DB::name("order")->where(['invite'=>['in',$invites]])->field('id,amount')->select();
            $count = count($infos);
            $price = 0;
            foreach($infos as $val){
                $price += $val['amount'];
            }

            $result = array("total" => $list->total(), "rows" => $list->items(),"extend" => ['num' => $count, 'price' => $price]);


            return json($result);
        }
        return $this->view->fetch();
    }

    public function add()
    {

        if ($this->request->isPost()) {
//            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    if (!Validate::is($params['mobile'], '1\d{1,12}')) {
                        exception(__("手机号格式不正确"));
                    }
                    $result = $this->model->save($params);
                    if ($result === false) {
                        exception($this->model->getError());
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    public function edit($ids = null)
    {
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }

        if ($this->request->isPost()) {
//            $this->token();
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    if (!Validate::is($params['mobile'], '1\d{1,12}')) {
                        exception(__("手机号格式不正确"));
                    }
//                    $result = $this->model->where('id',$row['id'])->save($params);
                    $result = $row->save($params);
                    if ($result === false) {
                        exception($this->model->getError());
                    }
                } catch (\Exception $e) {
                    $this->error($e->getMessage());
                }
                $this->success();
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign(['row'=>$row]);
        return $this->view->fetch();
    }

    public function addGroup(){
        $data = $this->request->post();
        $group_name = $data['name'];
        if(!$group_name){
            return $this->error("名称不能为空");
        }
        $pid = $data['pid'];
        Db::name("staff_group")->insert(['group_name'=>$group_name,'pid'=>$pid]);
        return $this->success('success');
    }

    public function editGroup(){
        $data = $this->request->post();
        $group_name = $data['name'];
        $pid = $data['pid'];
        $info = DB::name("staff_group")->where("id",$data['id'])->find();
        if($info['pid'] == 0){
            Db::name("staff_group")->where('id',$data['id'])->update(['group_name'=>$group_name]);
        }else{
            Db::name("staff_group")->where('id',$data['id'])->update(['group_name'=>$group_name,'pid'=>$pid]);
        }
        return $this->success('success');
    }

    public function delGroup(){
        $data = $this->request->post();
        DB::name("staff_group")->where("id",$data['id'])->delete();
        return $this->success('success');
    }

    public function groupList(){
        $list = DB::name("staff_group")->field('id,group_name as name')->select();
//        dump(json_encode($list));die;
        return json($list);
    }

    public function info($ids=null){
        $row = $this->model->get(['id' => $ids]);
        $fenzu_id = $row['group_id'];
        $list = DB::name("order")->where('invite',$row['invite'])->select();
        $this->view->assign("list",$list);
        return $this->view->fetch();
    }

//    /**
//     * 查看
//     */
//    public function GroupsList()
//    {
//        //设置过滤方法
//        $this->request->filter(['strip_tags', 'trim']);
//        $uid = $this->request->get('uid');
//
//        if ($this->request->isAjax()) {
//            //如果发送的来源是Selectpage，则转发到Selectpage
//            if ($this->request->request('keyField')) {
//                return $this->selectpage();
//            }
//            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
//
//            $list = $this->model
//                ->where($where)
//                ->where('uid',$uid)
//                ->order($sort, $order)
//                ->paginate($limit);
//
//            $result = array("total" => $list->total(), "rows" => $list->items());
//
//            return json($result);
//        }
//        return $this->view->fetch();
//    }

}
